<?php

namespace App\Models;

use Eloquent as Model;

/**
 * Class Organization
 * @package App\Models
 * @version November 14, 2017, 2:07 am UTC
 *
 * @property string name
 * @property in parent_id
 */
class ParkRecord extends Model
{

    protected $table = '';

    public $connection="park_cloud";

    public $timestamps=false;

    public function setTable($table)
    {
        $this->table = $table;

        return $this;
    }

    public $guarded=['id'];


    /**
     * 在场车统计
     *
     * @param null $park_code
     * @return mixed
     */
    static public function leaveFlow($park_code=null)
    {
        $parks = ParkInfo::active()->select('park_code');
        if($park_code){
            $parks = $parks->whereIn('park_code',$park_code);
        }

        $parks = $parks ->get();

        $car =$parks->map(function ($item){
            $table='t_park_record_'.$item->park_code;

            if(\Schema::connection('park_cloud')->hasTable($table)){
               $parkRecord =  new ParkRecord();
               $parkRecord->setTable($table);

                $item['inner_park'] = $parkRecord->where('status',1)->count();
            }else{
                $item['inner_park'] = 0;
            }
            return $item;
        });

        return $car;
    }

}
